using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Resources;
using System.Security;
using System.Web;
using PickGold.Charting;

namespace PickGold.Charting.Utilities
{
	internal class ImageLoader : IDisposable, IServiceProvider
	{
		private Hashtable _imageData;
		private IServiceContainer _serviceContainer;

		private ImageLoader()
		{
		}

		public ImageLoader(IServiceContainer container)
		{
			if (container == null)
			{
				throw new ArgumentNullException(SR.ExceptionImageLoaderInvalidServiceContainer);
			}
			this._serviceContainer = container;
		}

		public void Dispose()
		{
			if (this._imageData != null)
			{
				foreach (DictionaryEntry entry in this._imageData)
				{
					if (entry.Value is IDisposable)
					{
						((IDisposable)entry.Value).Dispose();
					}
				}
				this._imageData = null;
				GC.SuppressFinalize(this);
			}
		}

		internal static bool DoDpisMatch(Image image, Graphics graphics)
		{
			return ((graphics.DpiX == image.HorizontalResolution) && (graphics.DpiY == image.VerticalResolution));
		}

		internal static void GetAdjustedImageSize(Image image, Graphics graphics, ref SizeF size)
		{
			if (graphics != null)
			{
				size.Width = (image.Width * graphics.DpiX) / image.HorizontalResolution;
				size.Height = (image.Height * graphics.DpiY) / image.VerticalResolution;
			}
			else
			{
				size.Width = image.Width;
				size.Height = image.Height;
			}
		}

		internal bool GetAdjustedImageSize(string name, Graphics graphics, ref SizeF size)
		{
			Image image = this.LoadImage(name);
			if (image == null)
			{
				return false;
			}
			GetAdjustedImageSize(image, graphics, ref size);
			return true;
		}

		internal static Image GetScaledImage(Image image, Graphics graphics)
		{
			return new Bitmap(image, new Size((int)((image.Width * graphics.DpiX) / image.HorizontalResolution), (int)((image.Height * graphics.DpiY) / image.VerticalResolution)));
		}

		private Image LoadFromFile(string fileName)
		{
			try
			{
				return Image.FromFile(fileName);
			}
			catch (FileNotFoundException)
			{
				return null;
			}
		}

		public Image LoadImage(string imageURL)
		{
			return this.LoadImage(imageURL, true);
		}

		public Image LoadImage(string imageURL, bool saveImage)
		{
			Image image = null;
			if (this._serviceContainer != null)
			{
				Chart service = (Chart)this._serviceContainer.GetService(typeof(Chart));
				if (service != null)
				{
					foreach (NamedImage image2 in service.Images)
					{
						if (image2.Name == imageURL)
						{
							return image2.Image;
						}
					}
				}
			}
			if (this._imageData == null)
			{
				this._imageData = new Hashtable(StringComparer.OrdinalIgnoreCase);
			}
			if (this._imageData.Contains(imageURL))
			{
				image = (Image)this._imageData[imageURL];
			}
			if (image == null)
			{
				Chart chart2 = (Chart)this._serviceContainer.GetService(typeof(Chart));
				if ((chart2 != null) && (chart2.Page != null))
				{
					if (!chart2.IsDesignMode())
					{
						image = this.LoadFromFile(chart2.Page.MapPath(imageURL));
					}
					else if (chart2.IsDesignMode() && !string.IsNullOrEmpty(chart2.WebFormDocumentURL))
					{
						Uri uri = new Uri(chart2.WebFormDocumentURL);
						string localPath = uri.LocalPath;
						string oldValue = uri.Segments[uri.Segments.Length - 1];
						string newValue = chart2.ResolveClientUrl(imageURL);
						string fileName = localPath.Replace(oldValue, newValue);
						image = this.LoadFromFile(fileName);
					}
				}
				else if (HttpContext.Current != null)
				{
					image = this.LoadFromFile(HttpContext.Current.Request.MapPath(imageURL));
				}
			}
			if (image == null)
			{
				try
				{
					int index = imageURL.IndexOf("::", StringComparison.Ordinal);
					if (index > 0)
					{
						string baseName = imageURL.Substring(0, index);
						string name = imageURL.Substring(index + 2);
						ResourceManager manager = new ResourceManager(baseName, Assembly.GetExecutingAssembly());
						image = (Image)manager.GetObject(name);
					}
				}
				catch (MissingManifestResourceException)
				{
				}
			}
			if (image == null)
			{
				Uri requestUri = null;
				try
				{
					requestUri = new Uri(imageURL);
				}
				catch (UriFormatException)
				{
				}
				if (requestUri != null)
				{
					try
					{
						image = Image.FromStream(WebRequest.Create(requestUri).GetResponse().GetResponseStream());
					}
					catch (ArgumentException)
					{
					}
					catch (NotSupportedException)
					{
					}
					catch (SecurityException)
					{
					}
				}
			}
			if (image == null)
			{
				throw new ArgumentException(SR.ExceptionImageLoaderIncorrectImageUrl(imageURL));
			}
			if (saveImage)
			{
				this._imageData[imageURL] = image;
			}
			return image;
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		object IServiceProvider.GetService(Type serviceType)
		{
			if (serviceType != typeof(ImageLoader))
			{
				throw new ArgumentException(SR.ExceptionImageLoaderUnsupportedType(serviceType.ToString()));
			}
			return this;
		}
	}
}

